package pub.tbc.atps.process;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import pub.tbc.atps.config.HandlerStore;
import pub.tbc.atps.task.RestTask;
import pub.tbc.toolkit.Objs;

import java.util.LinkedList;

/**
 * // 遍历所有处理单元，依次执行
 *
 * @author tbc on 2016/11/29 12:43:17.
 */
@Slf4j
public class HandlerChain implements Runnable {
    @Setter
    @Getter
    private Object result;

    @Getter
    @Setter
    private HandlerContext context = new HandlerContext();

    public HandlerChain(RestTask task) {
        context.setAttribute("task", task);
    }

    // 处理器列表
    private LinkedList<Handler> getHandlers() {
        return HandlerStore.getHandlers();
    }

    //
    @Override
    public void run() {
        LinkedList<Handler> handlers = getHandlers();
        log.debug("Handler: {}", handlers);
        if (Objs.isEmpty(handlers))
            throw new RuntimeException("");

        for (Handler handler : handlers)
            if (!handler.handler(context)) break;
    }


}
